//
//  ModPasViewController.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/5/8.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "ModPasViewC.h"
#import "UserModel.h"
#import <MBProgressHUD.h>
#import "Util.h"

@interface ModPasViewC ()

@end

@implementation ModPasViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title =@"修改密码";
    self.navigationItem.rightBarButtonItem =
    [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"sureicon.png"]
                                     style:UIBarButtonItemStylePlain
                                    target:self
                                    action:@selector(save:)];
    _newPasField.secureTextEntry = YES;
    _oldPasField.secureTextEntry = YES;
    _anewPasField.secureTextEntry = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)save:(id)sender{
    
    NSString *oldpas = _oldPasField.text;
    NSString *newpas = _newPasField.text;
    NSString *anewpas = _anewPasField.text;
    
    [Util changeNSString:oldpas];
    [Util changeNSString:newpas];
    [Util changeNSString:anewpas];
    
    if (oldpas.length == 0 || newpas.length == 0 || anewpas.length == 0) {
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.labelText = @"请将资料填写完整";
        [HUD hide:YES afterDelay:1.5f];
        return ;
    }
    NSString *idNum =[NSString stringWithFormat:@"%@",
                      [UserModel sharedInstance].currentUser.ID];

    if ([newpas isEqualToString:anewpas]) {
        [[UserModel sharedInstance] personalModifyWithPassword:newpas ID:idNum perPas:^(BOOL successed, unsigned int value) {
            if (successed) {
                if (value == 0) {
                    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:
                                          self.view animated:YES];
                    HUD.labelText = @"密码修改成功";
                    [HUD hide:YES afterDelay:1.5f];
                    [self dismissViewControllerAnimated:YES
                     completion:^{}];
                 }
                else{
                    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                    HUD.labelText = @"密码修改失败";
                    [HUD hide:YES afterDelay:1.5f];
                }
            }
        }];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField;
{
    [textField resignFirstResponder];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
